<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      :row-class-name="tableRowClassName"
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>

      </el-table-column>
      <el-table-column label="名字" min-width="120">
        <template slot-scope="scope">
          {{ scope.row.name }}
          <div v-if="scope.row.french">
            {{ scope.row.french }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="介绍" width="430" align="center">
        <template slot-scope="scope">
          {{ scope.row.chineseBrief }}
        </template>
      </el-table-column>
      <el-table-column label="图片" width="150" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.img" alt="" style="width: 100px;">
        </template>
      </el-table-column>
      <el-table-column label="价格" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column label="销量" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.saleTotal }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="110" align="center">
        <template slot-scope="scope">
          <!-- {{ scope.row.lowerShelf?'已下架':'' }} -->
          <el-button v-if="scope.row.lowerShelf === 'true'" size="mini" type="warning" @click="handlTarger(scope.row)">下架中</el-button>
          <el-button v-else size="mini" type="success" @click="handlTarger(scope.row)">销售中</el-button>
        </template>
      </el-table-column>

      <el-table-column align="center" label="设置" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >设置</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-backtop />

    <!-- 编辑商品 -->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="价格" :label-width="formLabelWidth">
          <el-input v-model="form.price" autocomplete="off" />
        </el-form-item>
        <el-form-item label="是否下架" :label-width="formLabelWidth">
          <el-switch
            v-model="form.lowerShelf"
            :active-text="form.lowerShelf?'已下架':''"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdate">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 删除提示 -->
    <el-dialog
      :visible.sync="dialogVisible"
      width="30%"
    >
      <span>确定要删除这件商品吗，删除不可恢复！</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteDeter">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  inject: ['reload'],
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      dialogFormVisible: false,
      form: {
        objectId: '',
        name: '',
        price: '',
        lowerShelf: false
      },
      formLabelWidth: '120px',
      dialogVisible: false,
      objectId: '',
      listIndex: -1,
      deleteIndex: -1
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      this.$get('/1.1/classes/cake?where={"bcid":6}&order=-createdAt').then(res => {
        this.list = res.data.results
        // console.log(this.list)
        this.listLoading = false
      })
    },
    // 删除
    handleDelete(a, b) {
      this.deleteIndex = a
      this.objectId = b.objectId
      this.dialogVisible = true
    },
    // 关闭弹窗提示
    deleteDeter() {
      this.dialogVisible = false
      console.log(this.list[this.deleteIndex])
      this.list.splice(this.deleteIndex, 1)
      this.$delete(`/1.1/classes/cake/${this.objectId}`)
      // this.reload()
    },
    // 设置
    handleEdit(a, b) {
      this.listIndex = a
      this.dialogFormVisible = true
      this.form.objectId = b.objectId
      this.form.name = b.name
      this.form.price = b.price
      this.form.lowerShelf = (b.lowerShelf === 'true')
    },
    // 更新
    handleUpdate() {
      // this.dialogFormVisible = true
      // 修改信息
      this.$set(this.list[this.listIndex], 'name', `${this.form.name}`)
      this.$set(this.list[this.listIndex], 'price', `${this.form.price}`)
      this.$set(this.list[this.listIndex], 'lowerShelf', `${this.form.lowerShelf}`)

      this.$put(`/1.1/classes/cake/${this.form.objectId}`, { 'name': `${this.form.name}`, 'price': `${this.form.price}`, 'lowerShelf': `${this.form.lowerShelf}` })

      this.dialogFormVisible = false

      // this.reload()
    },
    tableRowClassName({ row, rowIndex }) {
      // console.log( );
      if (row.lowerShelf === 'true') {
        return 'info-row'
      } else {
        return ''
      }
    },
    // 下架按钮切换
    handlTarger(e) {
      if (e.lowerShelf === 'true') {
        this.$set(e, 'lowerShelf', 'false')
        this.$put(`/1.1/classes/cake/${e.objectId}`, { 'lowerShelf': `false` })
      } else {
        this.$set(e, 'lowerShelf', 'true')
        this.$put(`/1.1/classes/cake/${e.objectId}`, { 'lowerShelf': `true` })
      }
    }
  }
}
</script>
<style>
  .el-table .info-row {
    background: rgb(207, 202, 195);
  }
</style>
